package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.Exception.MyException;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.CheckResultService;
import com.itheima.health.service.OrderService;
import com.itheima.health.service.OrderSettingListService;
import com.itheima.health.service.SetmealService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.awt.print.Book;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *     预约列表页面
 * </p>
 * @Author：Super
 * @Date: 2020/12/6 10:49
 */
@RestController
@RequestMapping("/ordersettinglist")
public class OrderSettingListController {


    @Reference
    private OrderSettingListService orderSettingListService;

    @Reference
    private SetmealService setmealService;

    @Reference
    private OrderService orderService;

    @Reference
    private CheckResultService checkResultService;

    /**
     * 预约列表的分页查询
     *
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    @PreAuthorize("hasAuthority('ORDER_FINDPAGE')")
    public Result findPage(@RequestBody QueryPageBean queryPageBean, String option) {

        PageResult<Map<String, Object>> pageResult = orderSettingListService.findPage(queryPageBean,option);

        return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, pageResult);
    }


    /**
     * 取消预约
     * @param id
     * @return
     */
    @PostMapping("/cancelById")
    @PreAuthorize("hasAuthority('ORDER_CANCELBYID')")
    public Result cancelById(Integer id){

        orderSettingListService.cancelById(id);

        return new Result(true,"取消预约成功，且已成功发送取消短信给会员。");
    }


    /**
     * 已到诊
     * @param id
     * @return
     */
    @PostMapping("/arrive")
    @PreAuthorize("hasAuthority('ORDER_ARRIVE')")
    public Result arrive(Integer id){

        orderSettingListService.arrive(id);
        checkResultService.generatAdvice(id);
        return new Result(true,"更改状态为已到诊成功");

    }


    /**
     * 数据回显--查询所有套餐
     * @return
     */
    @GetMapping("/findAll")
    @PreAuthorize("hasAuthority('ORDER_FINDALL')")
    public Result findAll(){

        List<Setmeal> setmealList = setmealService.findAll();

        return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmealList);
    }


    /**
     * 添加预约
     * @return
     */
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('ORDER_ADD')")
    public Result add(@RequestBody Map<String,String> paramMap,Integer[] setmealIds){

        //判断线下预约的3个重要条件：电话，套餐id，预约日期
        if (setmealIds.length == 0) {
            throw new MyException("请选择一个预约的套餐。");
        }

        if (paramMap.get("telephone") == null) {
            throw new MyException("请填写预约的手机号码。");
        }

        if (paramMap.get("orderDate") == null) {
            throw new MyException("请选择一个正确的预约日期。");
        }

        //直接后台自动赋值预约类型
        paramMap.put("orderType","线下预约");

        //遍历所有预约的数据，进行批量添加
        for (Integer setmealId : setmealIds) {

            paramMap.put("setmealId", String.valueOf(setmealId));

            orderService.submit(paramMap);

        }

        return new Result(true,MessageConstant.ORDER_SUCCESS);
    }


    /**
     * 修改预约数据
     * update
     * @return
     */
    @RequestMapping("/update")
    @PreAuthorize("hasAuthority('ORDER_UPDATE')")
    public Result update(@RequestBody Map<String, String> paramMap,Integer setmealIds){

        //电话作为绑定会员的条件，所以不做更改，在这里判断套餐id非空，预约日期非空
        if (setmealIds == null) {
            throw new MyException("请选择一个预约的套餐。");
        }

            paramMap.put("setmealIds", String.valueOf(setmealIds));

            orderSettingListService.update(paramMap);

        return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);

    }






    /**
     * 通过id查询详细的预约数据
     * @param id
     * @return
     */
    @GetMapping("/findDetailById")
    @PreAuthorize("hasAuthority('ORDER_FINDBYID')")
    public Result findById(Integer id){

        Map<String,String> resultMap = orderSettingListService.findDetailById(id);

        return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,resultMap);

    }


    /**
     *  查询已选套餐
     * @param id
     * @return
     */
    @GetMapping("/findsetmealIdsByOrderId")
    @PreAuthorize("hasAuthority('ORDER_FINDSETMEALIDSBYORDERID')")
    public Result findsetmealIdsByOrderId(Integer id){

        List<Integer>  setmealIds = orderSettingListService.findsetmealIdsByOrderId(id);

        return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,setmealIds);

    }





}
